package controllers.security;

import java.util.Date;
import java.util.List;

import framework.base.BaseController;
import framework.base.PagerRS;
import framework.utils.DicUtil;
import framework.utils.SecurityUtil;
import framework.utils.StringUtil;
import models.security.Permission;
import models.security.Role;
import models.security.utils.SecurityConstant;
import models.security.vo.RoleQuery;
import net.sf.json.JSONArray;
import play.data.binding.ParamNode;
import play.data.validation.Valid;
import play.mvc.After;
import play.mvc.Controller;

public class Roles extends BaseController {
    /**
     * 角色列表
     */
    public static void index(Integer page,RoleQuery query){
        PagerRS<Role> rs = (PagerRS<Role>) Role.findByPager(getQuery(query, RoleQuery.class), getPageNo(page),
                getPageSize());
        render(rs);

    }
    /**
     * 角色新增
     */
    public static void add(){
        JSONArray array  = Permission.toMenuJson();
        render(array);
            
    }
    /**
     * 角色创建
     */
    public static void create(@Valid Role role){
        String pids = params.get("sele_pids");
        if(validation.hasErrors()){
           JSONArray array  = Permission.toMenuJson();
           saveUserLogAndNotice("角色创建失败", false);
           render("@add",array);
       }else{
          String loginerId = SecurityUtil.getLoginUserId();
          role.creatorId = loginerId;
          role.operatorId = loginerId;
          String[] arr = new String[0];
          if(!StringUtil.isBlank(pids)){
              arr = pids.split(",");
          }
          role.grant(arr);
          role.save(); 
          saveUserLogAndNotice("角色创建成功", true);
          index(null,null);
       }
    }
    
    /**
     * 角色编辑
     * @param id
     */
    public static void edit(String id){
        Role role = Role.findById(id);
        JSONArray array  = Permission.toMenuJson();
        String ids = null;
        List<Permission> pList = role.permissions;
        for(Permission p:pList){
            if(StringUtil.isBlank(ids)){
                ids = p.id+"";
            }else{
                ids = ids+","+p.id;
            }
        }
        render(role,array,ids);
    }
    
    /**
     * 角色更新
     */
    public static void update(String id){
       Role role = Role.findById(id);
       String pids = params.get("sele_pids");
       role.edit(ParamNode.convert(params.all()), "role");
       validation.valid(role);
       if(validation.hasErrors()){
           String ids = null;
           List<Permission> pList = role.permissions;
           JSONArray array  = Permission.toMenuJson();
           for(Permission p:pList){
               if(StringUtil.isBlank(ids)){
                   ids = p.id+"";
               }else{
                   ids = ids+","+p.id;
               }
           }
           saveUserLogAndNotice("角色更新失败", false);
           render("@edit",role,array,StringUtil.trim(ids));
       }else{
           String[] arr = new String[0];
           if(!StringUtil.isBlank(pids)){
               arr = pids.split(",");
               role.permissions.clear();
           }
           role.grant(arr);
           String loginerId = SecurityUtil.getLoginUserId();
           role.operatorId = loginerId;
           role.updatedAt = new Date();
           role.save();
           saveUserLogAndNotice("角色更新成功", true);
           index(null,null);
       }

    }
    /**
     * 角色删除
     */
    public static void destroy(String id){
        Role role = Role.findById(id);
        if(null!=role){
            String loginerId = SecurityUtil.getLoginUserId();
            role.operatorId = loginerId;
            role.destroy();
            saveUserLogAndNotice("角色删除成功", true);
        }else{
            saveUserLogAndNotice("角色删除失败", false);
        }
        index(null,null);
        
    }

    /**
     * 角色查看
     */
    public static void show(String id){
        Role role = Role.findById(id);
        JSONArray array = role.toMenuJson();
        String ids = null;
        List<Permission> pList = role.permissions;
        for(Permission p:pList){
            if(StringUtil.isBlank(ids)){
                ids = p.id+"";
            }else{
                ids = ids+","+p.id;
            }
        }
        render(role,array,ids);
    }
    
    @After(only = { "security.Roles.create", "security.Roles.update", "security.Roles.destroy" })
    public static void lifeCycle() {
            DicUtil.loadByDicName(SecurityConstant.DIC_ROLE_LIST_NAME);
    }
}
